package org.ushio.nchu_22201402.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.ushio.common_module.entity.User;
import org.ushio.common_module.entity.UserBanTime;
import org.ushio.nchu_22201402.Foen.Admin_UserFoen;
import org.ushio.nchu_22201402.service.Admin_UserService;

import javax.annotation.Resource;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping(value = "/AdminUser")
public class Admin_UserController {


    @Autowired
    private Admin_UserFoen adminUserFoen;

    @Autowired
    private Admin_UserService adminUserService;

    @GetMapping(value ="/ListUser")

    public List<User> findAllUsers(){
        System.out.println("0");
        return  adminUserFoen.findAllUsers();
    }

    @GetMapping(value = "/findUserByphoneNumber")
    public User getUserByPhoneNumber(@RequestParam String phoneNumber){
        return adminUserFoen.getUserByPhoneNumber(phoneNumber);
    }

    @PostMapping(value = "/BanUser")
    String Ban_Record(@RequestBody UserBanTime userBanTime){
       if(adminUserFoen.getUserByPhoneNumber(userBanTime.getPhone_Number())!=null) {
           adminUserService.Ban_User(userBanTime.getPhone_Number());
           return adminUserService.Ban_Record(userBanTime.getPhone_Number(), userBanTime.getBan_Time(), userBanTime.getBan_Reason());
       }
       else {
           return "该用户不存在";
       }

    }

    @GetMapping(value = "/ListUserBan")
    List<UserBanTime> ListUserBan(){
        return adminUserService.ListUserBan();
    }



    @PostMapping(value="/releaseUser")
    String releaseUser(@RequestBody User user){
        if(adminUserFoen.getUserByPhoneNumber(user.getPhoneNumber())!=null){
            {
                if (adminUserFoen.getUserByPhoneNumber(user.getPhoneNumber()).getBanStatus() == 1) {
                    adminUserService.releaseUser(user.getPhoneNumber());
                    return "success";
                } else {
                    return "fail";
                }
            }
    }else{
            return "fail";
        }
    }

    //供22201408调用
    @GetMapping(value = "/getBanIfo")
    List<UserBanTime> getBanIfo(@RequestParam String Phone_Number){
        return adminUserService.getBanIfo(Phone_Number);
    }

}
